package game.module.mj.processor;

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.HallServer;
import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.mj.bean.MjRoom;
import game.module.mj.logic.MjDataManager;
import game.session.SessionManager;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageLogin.S2ErrorCode;
import lua.LuaMessageMj.C2SMJReady;
import lua.LuaMessageMj.PushMjReady;
import lua.LuaMessageMj.S2CMJReady;

/**
 * 
 * @author llee
 *
 */
@MsgCodeAnn(msgcode = C2SMJReady.id, accessLimit = 500)
public class MjReadyProcessor extends PlayingRoleMsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(MjReadyProcessor.class);

	@Override
	public void process(PlayingRole playingRole, RequestMessageRaw requestMessage) throws Exception {

		int playerId = playingRole.getId();
        
		logger.info("mj  player ready,player={}",playerId);
		
		boolean isInRoom = MjDataManager.getInstance().isPlayerInRoom(playerId);
		if(!isInRoom){
			S2ErrorCode respMsg = new S2ErrorCode(106);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		MjRoom  mjroom = MjDataManager.getInstance().getMjRoomByPlayerId(playerId);
		byte readystate[] = null;
		List<Integer> player = null;
		synchronized(mjroom) {
			 readystate = mjroom.getReadyState();
			 player = mjroom.getPlayerList();
			readystate[player.indexOf(playerId)] = 1;
		}
		S2CMJReady msg = new S2CMJReady();
		playingRole.writeAndFlush(msg.build(playingRole.alloc()));
		PushMjReady pushmsg = new PushMjReady();
		pushmsg.player_index = (byte) player.indexOf(playerId);
		for (Integer aPlayerId : player) {
			if (aPlayerId == playerId) {
				continue;
			}
			PlayingRole pr = SessionManager.getInstance().getPlayer(aPlayerId);
			if (pr != null && pr.isChannelActive()) {
				pr.writeAndFlush(pushmsg.build(pr.alloc()));
			}
		}
		for(byte a :readystate) {
			if(a ==0)
				return;
		}
		sendRoomStart(mjroom);
	
	}
	private void sendRoomStart(MjRoom mjRoom) {
		logger.info("mj start battle!");
		String rsLanHost = HallServer.getInstance().getServerConfig().getRsLanHost();
		int rsLanPort = HallServer.getInstance().getServerConfig().getRsLanPort();
		boolean reconnectRet = HallServer.getInstance().getLanClientManager().connect(rsLanHost, rsLanPort);
		if (reconnectRet) {
			HallServer.getInstance().getLanClientManager().sendMjStart2Rs(rsLanHost, rsLanPort, mjRoom);
		} else {
			logger.error("connect 2 roomServer error!rsLanHost={},rsLanPort={}", rsLanHost, rsLanPort);
		}
	}
	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		// TODO Auto-generated method stub

	}

}
